package net.esj.basic.plugins.hibernate.id;

import java.io.Serializable;
import java.util.Properties;

import net.esj.basic.utils.DateUtils;

import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.id.Configurable;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.type.Type;

public class TimeAndRandomIdGenerator implements IdentifierGenerator,
		Configurable {

	private static java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
	"yyyyMMddHHmmssSSS");//17wei
	
	private static final char[] RN_NUMBER = {'0','1','2','3','4','5','6','7','8','9'};
	
	private String token = "UDFIS";
	
	@Override
	public Serializable generate(SessionImplementor arg0, Object arg1)
			throws HibernateException {
		StringBuilder sb = new StringBuilder();
		sb.append(formatter.format(DateUtils.now()));
		/*sb.append(formatter.format(DateUtils.now())).append(token);
		sb.append(RandomStringUtils.random(10, RN_NUMBER));*/
		return sb.toString();
	}

	@Override
	public void configure(Type arg0, Properties params, Dialect arg2)
			throws MappingException {
		token = params.getProperty("token", token);
		token = token.substring(0, 5);
	}
}
